import axios from 'axios'
import { getToken, setToken, removeToken } from '@/utils/auth';

// 创建axios实例
const HTTP = axios.create({
    baseURL: '/gpt/back',
    timeout: 60000

})

// 请求拦截
HTTP.interceptors.request.use(config => {
    //设置请求头信息
    let token = getToken();
    if (token) {
        const headersOption = {
            "Authorization": "Bearer " + token
        }
        config.headers = Object.assign(config.headers, headersOption);
    }
    return config;
}, err => {
    console.log("请求拦截错误 ");
    console.log(err);
})

// 响应拦截
HTTP.interceptors.response.use(res => {
    return new Promise((resolve, reject) => {
        if (res.status === 200) {
            // 可以继续细分接口返回res.data.status/res.data.code状态码的判断
            if(res.data.state==-3){//没有权限
                window.location.replace('/login');
            }
            resolve(res.data);
        } else {
            reject(res)
        }
    });
}, err => {
    console.log(err);
})

export default HTTP
